package com.base.configuration.action;

import static com.base.common.GlobaConstants.ACTION_RESULT_DATA;
import static com.base.common.GlobaConstants.ASYNCHRONOUS;
import static com.base.common.GlobaConstants.CONTENT_TYPE;
import static com.base.common.GlobaConstants.CONTENT_TYPE_JSON;
import static com.base.common.GlobaConstants.JSON;
import static com.base.common.GlobaConstants.ROOT;

import javax.annotation.Resource;

import com.base.common.utils.StringUtils;
import org.apache.struts2.components.If;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.base.common.action.AbstractBaseAction;
import com.base.common.bean.ActionResult;
import com.base.common.enums.ConfigKey;
import com.base.common.persistent.model.Configuration;
import com.base.configuration.service.software.SoftwareService;
import com.cjy.common.bean.OpAttributes;

/**
 * 程序软件管理
 * 
 * @author Administrator
 *
 */

@Action("softwareManage")
@Results({ @Result(name = ASYNCHRONOUS, type = JSON, params = { ROOT,
		ACTION_RESULT_DATA, CONTENT_TYPE, CONTENT_TYPE_JSON }) })
public class SoftwareManage extends AbstractBaseAction {

	private static final long serialVersionUID = 1L;

	@Resource
	private SoftwareService softwareService;

	private int bid;

	private String version;

	private int compoundsId;

    public int getCompoundsId() {
        return compoundsId;
    }

    public void setCompoundsId(int compoundsId) {
        this.compoundsId = compoundsId;
    }

    public int getBid() {
		return bid;
	}

	public void setBid(int bid) {
		this.bid = bid;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	/**
	 * 获取app最新版本下载地址
	 * 
	 * @return
	 */
	public String getLatestVersion() {
		try {
			Configuration configuration = new Configuration();
//			Configuration config = null;
            String lastedVersion = null;
			int flag = 1;

			// 判断是哪种app
			if (bid == 0) {
				configuration.setConfigKey(ConfigKey.CJY_ANDROID_VERSION
						.getValue().trim());
//				config = softwareService.query(configuration);
				configuration.setConfigKey(ConfigKey.CJY_ANDROID_URL.getValue()
						.trim());

			} else if (bid == 3) {
				configuration.setConfigKey(ConfigKey.CJY_IOS_VERSION.getValue()
						.trim());
//				config = softwareService.query(configuration);
				configuration.setConfigKey(ConfigKey.CJY_IOS_URL.getValue()
						.trim());
			} else if (bid == 1) {
				configuration.setConfigKey(ConfigKey.YGJ_ANDROID_VERSION
						.getValue().trim());
//				config = softwareService.query(configuration);
				configuration.setConfigKey(ConfigKey.YGJ_ANDROID_URL.getValue()
						.trim());
			} else if (bid == 4) {
				configuration.setConfigKey(ConfigKey.YGJ_IOS_VERSION.getValue()
						.trim());
//				config = softwareService.query(configuration);
				configuration.setConfigKey(ConfigKey.YGJ_IOS_URL.getValue()
						.trim());
			} else if (bid == 5){
//				config = softwareService.query(configuration);
				configuration.setConfigKey(ConfigKey.JYGJ_ANDROID_URL.getValue()
						.trim());
				flag = 2;
			} else if (bid == 6){
//				config = softwareService.query(configuration);
				configuration.setConfigKey(ConfigKey.JYGJ_IOS_URL.getValue()
						.trim());
				flag = 2;
			}

			try {
				lastedVersion = softwareService.queryLastedVersion(flag, compoundsId);
			} catch (Exception e) {
				e.printStackTrace();
				actionResult = ActionResult.getActionResult(new OpAttributes()
						.add("code", 0));
			}
			// 判断用户当前版本是否是最新版本
			if (lastedVersion != null
					&& !lastedVersion.trim().equals("")
					&& compareVersion(version.trim(), lastedVersion.trim())) {
				actionResult = ActionResult
						.getActionResult(new OpAttributes().add("code", 1)
								.add("result",
										new OpAttributes().add(
												"url",
												softwareService.query(
														configuration)
														.getConfigValue())));
			} else {

				actionResult = ActionResult.getActionResult(new OpAttributes()
						.add("code", 0));
			}

		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

    public String selectAppFlag() {
        try {
            actionResult = ActionResult.getActionResult(new OpAttributes().add(
                    "code", 1).add("result", softwareService.selectAppFlag(compoundsId)));
        } catch (Exception e) {
            e.printStackTrace();
            actionResult = ActionResult.getActionResult(new OpAttributes().add(
                    "code", 0));
        }
        return returnType();
    }

    private boolean compareVersion(String version1, String version2) throws Exception{
        boolean flag = false;
		System.out.println(version1.replace(".", ""));
		if (StringUtils.isNotEmptyByTrim(version1) && StringUtils.isNotEmptyByTrim(version2)){
            int i1 = Integer.valueOf(version1.replace(".", ""));
            int i2 = Integer.valueOf(version2.replace(".", ""));
            if (i1 < i2) {
                flag = true;
            }
        }
        return flag;
    }

}
